extends Control

# 引用UI组件
@onready var icon = $CoinIcon
@onready var count_label = $CountLabel

# 动画变量
var current_coins = 0

func _ready():
	# 初始化标签
	count_label.text = "0"
	
	# 获取玩家节点
	var player = get_node("/root/Main/Player")

func _process(_delta):
	# 从玩家数据中获取金币数量并更新UI
	var player = get_node("/root/Main/Player")
	if player and player.has_method("get_stats"):
		var stats = player.get_stats()
		if stats and stats.has_method("has_property") and stats.has_property("coins"):
			var coins = stats.coins
			if coins > current_coins:
				_play_coin_animation()
				current_coins = coins
			_update_count(coins)

func _update_count(amount: int):
	# 更新金币数量标签
	count_label.text = str(amount) 

func _play_coin_animation():
	# 创建Q弹效果的动画
	var bounce_tween = create_tween()
	# 快速放大到1.3倍
	bounce_tween.tween_property(icon, "scale", Vector2(1.3, 1.3), 0.15)
	# 然后缓慢恢复原始大小
	bounce_tween.tween_property(icon, "scale", Vector2(1, 1), 0.15) 
